import subprocess
import logging

logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logger = logging.getLogger(__name__)


def play_video(file_path, volume=1.0):

    logger.info("播放视频：{}".format(file_path))

    cmd = ["gst-play-1.0", "--gapless", "--volume={}".format(volume), file_path]

    subprocess.run(cmd)

def stop_video():

    cmd = "killall gst-play-1.0"

    subprocess.Popen(cmd, shell=True)



if __name__ == "__main__":

    import os
    from setting import AD_VOLUME

    import redis

    r = redis.Redis(host='localhost', port=6379, db=0)
    
    ad_volume = r.get(AD_VOLUME)

    if ad_volume:

        ad_volume = float(ad_volume.decode())

    else:

        ad_volume = 1.0

    ad_path = "/home/linaro/Videos/菁英茶三分钟版本-1080p-1124.mp4" # 默认的广告

    if os.path.exists("/home/linaro/Videos/.play_ad_file"):

        with open("/home/linaro/Videos/.play_ad_file", encoding='utf-8') as f:

            now_ad_path = f.read()

            if os.path.exists(now_ad_path):

                ad_path = now_ad_path
    
    play_video(ad_path, ad_volume)
